﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SocialMediaProviderFactory.cs" company="Catel development team">
//   Copyright (c) 2008 - 2012 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catwitter.Factories
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using Catel.IoC;
    using Providers;

    using Catel.Reflection;

    /// <summary>
    /// Factory responsible to create all <see cref="ISocialMediaProvider"/> instances.
    /// </summary>
    public class SocialMediaProviderFactory : ISocialMediaProviderFactory
    {
        #region Variables
        private static readonly List<ISocialMediaProvider> _providers = new List<ISocialMediaProvider>();
        #endregion

        #region Methods
        /// <summary>
        /// Gets the <see cref="ISocialMediaProvider"/> instances in the current working directory.
        /// </summary>
        /// <returns><see cref="IEnumerable{ISocialMediaProvider}"/> containing all providers in the current working directory.</returns>
        public IEnumerable<ISocialMediaProvider> GetProviders()
        {
            lock (_providers)
            {
                if (_providers.Count > 0)
                {
                    return _providers;
                }

                try
                {
                    var providers = TypeHelper.GetTypes(x => !x.IsAbstractEx() && !x.IsInterfaceEx() && x.ImplementsInterfaceEx(typeof(ISocialMediaProvider)));
                    foreach (var providerType in providers)
                    {
                        _providers.Add((ISocialMediaProvider)DependencyInjectionHelper.CreateInstance(providerType));
                    }
                }
                catch (Exception)
                {
                   // Swallow exceptions
                }

                return _providers;
            }
        }
        #endregion
    }
}